package behavioral.memento.blackbox;

/**
 * 这是黑盒备忘录的实现方式，他自己内部实现了备忘录接口，保障了自己的备忘录不会被他人读取
 */
public class Originator {

    private String state;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    /**
     * 创建一个备忘录，但是是接口类型的返回值，能够保证该备忘录不被外部读取
     * @return
     */
    public IMemento createMemento() {
        return new CustomeMemento(state);
    }

    /**
     * 内部强转，只有它自己知道具体备忘录的类型，保证了安全
     * @param iMemento
     */
    public void restoreMemento(IMemento iMemento) {
        this.state = ((CustomeMemento)iMemento).getState();
    }


    class CustomeMemento implements IMemento {
        private String state;

        public CustomeMemento(String state) {
            this.state = state;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }
    }
}
